import Vue from 'vue'
import VueRouter from 'vue-router'
import userlist from '@/views/user/userlist'
import useradd from '@/views/user/useradd'
import index from '@/views/index'
import platform from '@/views/platform'
import shop from '@/views/shop'
import operator from '@/views/operator'
import role from '@/views/user/role'
import shoplist from '@/views/shop/shoplist'
import authlist from '@/views/auth/authlist'
import authlistadd from '@/views/auth/authlistadd'
import goodsList from '@/views/shop/goodsList'
import goodsAdd from '@/views/shop/goodsAdd'
Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/index',
    },
    {
        path: '/index',
        component: () =>
            import ('../views/home.vue'),
        children: [{
                path: '/index',
                component: index,
                children: [{
                        path: '/user',
                        component: userlist
                    },
                    {
                        path: '/role',
                        component: role
                    }
                ]
            },
            {
                path: '/platform',
                component: platform,
                children: [{
                        path: '/authlist',
                        component: authlist,
                    }, {
                        path: '/authlistadd',
                        component: authlistadd
                    }

                ]
            },
            {
                path: '/shop',
                component: shop,
                children: [{
                    path: '/goodsList',
                    component: goodsList
                }, {
                    path: '/goodsAdd',
                    component: goodsAdd
                }]
            },
            {
                path: '/operator',
                component: operator
            }
        ]

    },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('../views/Login.vue')
    }
]

const router = new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes
    })
    // 导航守卫
router.beforeEach((to, from, next) => {
    // to 即将要进入的目标 路由对象
    // from  当前导航正要离开的路由
    // next 方法的调用参数
    if (to.path == '/login') return next();
    // 判断是否登录
    // 取sessionStorage里面的值
    const name = window.sessionStorage.getItem('name');
    if (!name) {
        // 如果没有登录，到登录页面
        return next('/login');
    } else {
        // 如果登录 next 
        next();
    }


})
export default router